import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'category',
      label: $t('views.infra.config.category'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.config.categoryPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'name',
      label: $t('views.infra.config.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.config.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'key',
      label: $t('views.infra.config.key'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.config.keyPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'value',
      label: $t('views.infra.config.value'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.config.valuePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'visible',
      label: $t('views.infra.config.visible'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING, 'boolean'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: true,
      rules: 'required',
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.infra.config.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.config.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'key',
      label: $t('views.infra.config.key'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.infra.config.keyPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'type',
      label: $t('views.infra.config.type'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.INFRA_CONFIG_TYPE, 'number'),
        placeholder: $t('views.infra.config.typePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: $t('views.common.id'),
      minWidth: 100,
    },
    {
      field: 'category',
      title: $t('views.infra.config.category'),
      minWidth: 120,
    },
    {
      field: 'name',
      title: $t('views.infra.config.name'),
      minWidth: 200,
    },
    {
      field: 'key',
      title: $t('views.infra.config.key'),
      minWidth: 200,
    },
    {
      field: 'value',
      title: $t('views.infra.config.value'),
      minWidth: 150,
    },
    {
      field: 'visible',
      title: $t('views.infra.config.visible'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_BOOLEAN_STRING },
      },
    },
    {
      field: 'type',
      title: $t('views.infra.config.type'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.INFRA_CONFIG_TYPE },
      },
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 150,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
